package com.aiapphub.SystemCode.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.aiapphub.SystemCode.entity.DifyConfig;
import com.aiapphub.SystemCode.mapper.DifyConfigMapper;
import com.aiapphub.SystemCode.service.DifyConfigService;
import org.springframework.stereotype.Service;
import java.util.Date;
import java.util.List;

/**
 * Dify配置服务实现类
 */
@Service
public class DifyConfigServiceImpl extends ServiceImpl<DifyConfigMapper, DifyConfig> implements DifyConfigService {

    @Override
    public List<DifyConfig> listAllConfigs() {
        return baseMapper.selectList(new QueryWrapper<DifyConfig>().orderByAsc("display_order"));
    }

    @Override
    public List<DifyConfig> listEnabledConfigs() {
        return baseMapper.selectList(new QueryWrapper<DifyConfig>()
                .eq("status", 1)
                .orderByAsc("display_order"));
    }

    @Override
    public DifyConfig getConfigById(Long id) {
        return baseMapper.selectById(id);
    }

    @Override
    public boolean saveConfig(DifyConfig config) {
        // 设置默认值
        if (config.getDisplayOrder() == null) {
            config.setDisplayOrder(0);
        }
        if (config.getStatus() == null) {
            config.setStatus(1);
        }
        // 手动设置时间字段
        Date now = new Date();
        config.setCreateTime(now);
        config.setUpdateTime(now);
        return save(config);
    }

    @Override
    public boolean updateConfig(DifyConfig config) {
        // 手动设置更新时间
        config.setUpdateTime(new Date());
        return updateById(config);
    }

    @Override
    public boolean deleteConfig(Long id) {
        return removeById(id);
    }

    @Override
    public boolean updateStatus(Long id, Integer status) {
        DifyConfig config = new DifyConfig();
        config.setId(id);
        config.setStatus(status);
        return updateById(config);
    }
}